package com.people.netmon.reddit.probe.service.impl;

import ai.people.netmon.framework.domain.reddit.entity.RedditAccount;
import ai.people.netmon.framework.domain.reddit.request.RedditRequestParameter;
import ai.people.netmon.framework.domain.reddit.vo.RedditAccountVO;
import ai.people.netmon.framework.exception.enums.AccountExceptionEnum;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.utils.AssertUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.people.netmon.reddit.probe.mapper.AccountMapper;
import com.people.netmon.reddit.probe.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * reddit用户服务impl
 *
 * @author yuanqinglong
 * @date 2022/3/8 13:25
 */
@Service
public class AccountServiceImpl implements AccountService {


    @Autowired
    private AccountMapper accountMapper;


    /**
     * 通过用户名获取用户
     *
     * @param username 用户名
     * @return {@link RedditAccount}
     */
    @Override
    public RedditAccountVO getUserByUsername(String username) {
        LambdaQueryWrapper<RedditAccount> lambdaQuery = Wrappers.lambdaQuery(RedditAccount.class);
        lambdaQuery.eq(RedditAccount::getUserName, username);
        RedditAccount redditAccount = accountMapper.selectOne(lambdaQuery);
        AssertUtils.isNotNull(redditAccount, CommonEnum.QUERY_NOT_FOUND);
        return new RedditAccountVO(redditAccount);
    }

    /**
     * 获得账户
     *
     * @param username 用户名
     * @return {@link RedditAccount}
     */
    @Override
    public RedditAccount getAccount(String username) {
        LambdaQueryWrapper<RedditAccount> lambdaQuery = Wrappers.lambdaQuery(RedditAccount.class);
        lambdaQuery.select(RedditAccount::getHeaderImage,RedditAccount::getBackgroundImage)
                .eq(RedditAccount::getUserName,username);
        RedditAccount redditAccount = accountMapper.selectOne(lambdaQuery);
        AssertUtils.isNotNull(redditAccount, AccountExceptionEnum.ACCOUNT_DOES_NOT_EXIST);
        return redditAccount;
    }


    /**
     * 用户列表
     *
     * @param redditRequestParameter 请求参数
     * @return {@link PageResponse}<{@link RedditAccountVO}>
     */
    @Override
    public PageResponse<RedditAccountVO> listUsers(RedditRequestParameter redditRequestParameter) {
        IPage<RedditAccount> page = new Page<>(redditRequestParameter.getOriginPageNo(), redditRequestParameter.getPageSize());
        LambdaQueryWrapper<RedditAccount> lambdaQuery = Wrappers.lambdaQuery(RedditAccount.class);
        IPage<RedditAccount> selectPage = accountMapper.selectPage(page, lambdaQuery);
        List<RedditAccountVO> userVOList = selectPage.getRecords().stream().map(RedditAccountVO::new).collect(Collectors.toList());
        return PageResponse.builder(userVOList, selectPage.getTotal(), selectPage.getPages(), selectPage.getCurrent());
    }
}
